package com.yxq.mdt.app.modules.spikedetail.ui;


import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.Nullable;

import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseFragment;
import com.yxq.mdt.app.modules.market.bean.MallGoodsBean;
import com.yxq.mdt.app.modules.spikedetail.bean.GoodsCommentBean;
import com.yxq.mdt.app.modules.spikedetail.bean.SpikeGoodsDetailBean;
import com.yxq.mdt.app.modules.spikedetail.model.SpikeGoodsModel;
import com.yxq.mdt.app.modules.spikedetail.presenter.SpikeGoodsPresenter;
import com.yxq.mdt.app.modules.spikedetail.presenter.ISpikeGoodsContract;
import com.yxq.mdt.app.utils.StringUtils;

import butterknife.BindView;

/**
 * @author L
 * @time
 * @describe 商品详情-详情
 */
public class SpikeGoodsDetailDetailFragment extends BaseFragment<SpikeGoodsPresenter> implements ISpikeGoodsContract.View {


    @BindView(R.id.goods_detail_web_view)
    WebView goodsDetailWebView;
    WebSettings webSettings;

    @Override
    protected int attachLayoutId() {
//        setImmersionBarEnabled(false);
        return R.layout.fragment_goods_detail_detail;
    }

    @Override
    protected SpikeGoodsPresenter getPresenter() {
        return new SpikeGoodsPresenter(this, new SpikeGoodsModel());
    }

    @Override
    public void initView(View view) {
        webSettings = goodsDetailWebView.getSettings();
        //启用JavaScript
        webSettings.setJavaScriptEnabled(true);
        //允许缩放
        webSettings.setSupportZoom(true);
        //不使用缓存
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        //扩大比例的缩放
        webSettings.setUseWideViewPort(true);
        //自适应屏幕
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setLoadWithOverviewMode(true);
        goodsDetailWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });

    }

    @Override
    public void initData() {
        mPresenter.getGoods(getArguments().getString("id"));
    }

    @Override
    public void setData(@Nullable Object data) {

    }


    @Override
    public void onLoadSuccess(SpikeGoodsDetailBean goodsDetailBean) {
        if (goodsDetailBean == null) {
            return;
        }

        bindWebView(goodsDetailBean);
    }

    /**
     * 初始化商品详情webview
     *
     * @param goodsDetailBean
     */
    private void bindWebView(SpikeGoodsDetailBean goodsDetailBean) {
        if (goodsDetailBean != null && !TextUtils.isEmpty(goodsDetailBean.getDetail())) {
            goodsDetailWebView.loadDataWithBaseURL("", StringUtils.getNewData(goodsDetailBean.getDetail()), "text/html", "UTF-8", null);
        }
    }

    @Override
    public void onLoadFailed(String err) {

    }

    @Override
    public void onLoadRecommendSuccess(MallGoodsBean mallGoodsBean) {

    }

    @Override
    public void onLoadRecommendFailed(String err) {

    }

    @Override
    public void onAddSuccess(String err) {

    }

    @Override
    public void onAddFailed(String err) {

    }

    @Override
    public void onRemoveSuccess(String err) {

    }

    @Override
    public void onRemoveFailed(String err) {

    }

    @Override
    public void onLoadCommentSuccess(GoodsCommentBean goodsCommentBean) {

    }

    @Override
    public void onLoadCommentFailed(String err) {

    }

    @Override
    public void onAddCartSuccess(String result) {

    }

    @Override
    public void onAddCartFailed(String err) {

    }
}